package com.jnks.shardjdbc;


import com.jnks.constant.enums.ViewTypeEnum;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

public class SubDataBaseDronePreciseAlgorithm implements PreciseShardingAlgorithm<Integer> {


    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Integer> shardingValue) {
        for (String databaseName : availableTargetNames) {
            Integer provinceId = shardingValue.getValue();
            provinceId = provinceId == null  ? 0 : provinceId;
            if (databaseName.endsWith(ViewTypeEnum.getCodeValue(provinceId))) {
                return databaseName;
            }
        }
        throw new IllegalArgumentException();
    }
}
